﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class JetFlower : MonoBehaviour
{
    public float Distance;//检测距离
    public LayerMask PlayerLayer;
    public GameObject Jet;//发射物体
    public Transform JetPoint;

    public float X, Y;//方形检测大小

    private int Num;

    private float yOffset;
    private Collider2D coll;
    Vector2 pos;


    private Animator anim;
    void Start()
    {
        coll = GetComponent<Collider2D>();
        anim = GetComponent<Animator>();
        yOffset = coll.bounds.size.y;
    }

    void Update()
    {
        PhysicsCheck();
    }
    void PhysicsCheck()
    {
        pos = transform.position;
        //方形检测
        RaycastHit2D[] hitPlayers = Physics2D.BoxCastAll(pos + new Vector2(0, yOffset / 2), new Vector2(X, Y), Distance, Vector2.up, PlayerLayer);
       // Debug.Log(hitPlayers.Length);
       

        //默认Length = 1，所以是3不是2
        if (hitPlayers.Length == 5)//当两个人站着
        {
            anim.SetTrigger("press");
            Num = 5;
        }
        if (Num - hitPlayers.Length == 2)//少一个人
        {
            Debug.Log("发射");
            anim.SetTrigger("jet");
            Instantiate(Jet, JetPoint.position, Quaternion.identity);//发射推动物体
            Num = hitPlayers.Length;
        }
    }
}
